package ch.ethz.fcl.metrobuzz.data.interchange;

import java.util.ArrayList;

import ch.ethz.fcl.metrobuzz.render.techniques.colors.MBColorTable;

class InterchangeColor {
	protected static int pickColorRange(Interchange i1, Interchange i2) {
		int[] colors = new int[MBColorTable.COLORS];
		for (int i = 0; i < colors.length; i++)
			colors[i] = i;

		if (i1 != null)
			for (Integer color : i1.getArcElementColors())
				if (color != -1) {
					colors[getColorRange(color)] = -1;
				}
		if (i2 != null)
			for (Integer color : i2.getArcElementColors())
				if (color != -1)
					colors[getColorRange(color)] = -1;

		ArrayList<Integer> avaColors = new ArrayList<Integer>();
		for (int i = 0; i < colors.length; i++)
			if (colors[i] != -1)
				avaColors.add(colors[i]);

		if (avaColors.size() == 0)
			return 0;

		int rand = Math.min((int) (Math.random() * avaColors.size()),
				avaColors.size() - 1);
		return avaColors.get(rand);
	}

	protected static int pickColorInRange(int colorCode, Interchange i1, Interchange i2) {
		int cc = getColorRange(colorCode);
		int[] colors = new int[10];
		for (int i = 0; i < colors.length; i++)
			colors[i] = i;

		if (i1 != null)
			for (Integer color : i1.getArcElementColors())
				if (color != -1 && getColorRange(color) == cc && color >= 10)
					colors[color % 10] = -1;

		if (i2 != null)
			for (Integer color : i2.getArcElementColors())
				if (color != -1 && getColorRange(color) == cc && color >= 10)
					colors[color % 10] = -1;

		for (int i = 0; i < colors.length; i++)
			if (colors[i] != -1) {
				cc = (cc + 1) * 10 + colors[i];
				break;
			}

		return cc;
	}

	protected static int getColorRange(int colorIndex) {
		if (colorIndex < 10)
			return colorIndex;
		else
			return (colorIndex / 10) - 1;
	}
	
}
